<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page import="weaver.alioss.AliOSSObjectManager" %>
<%@ page import="weaver.conn.ConnStatement" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.file.AESCoder" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.zip.ZipInputStream" %>
<%@ page import="weaver.general.Base64" %>
<%
    InputStream inputStream = getInputStreamById(5);
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    for (int length; (length = inputStream.read(buffer)) != -1; ) {
        result.write(buffer, 0, length);
    }
    byte[] finalBytes = result.toByteArray();
    byte[] byteEncode = Base64.encode( finalBytes );
    out.print( new String(byteEncode));
    InputStream inputStream1 = new ByteArrayInputStream(Base64.decode( byteEncode ));
    String fileName="D:\\WEAVER\\ecology\\interface\\both\\test.jpg";
    File file=new File(fileName);
    if(!file.exists()){
        file.createNewFile();
    }
    BufferedInputStream in=null;
    BufferedOutputStream outputStream=null;
    in=new BufferedInputStream(inputStream1);
    outputStream=new BufferedOutputStream(new FileOutputStream(fileName));
    int len=-1;
    byte[] b=new byte[1024];
    while((len=in.read(b))!=-1){
        outputStream.write(b,0,len);
    }
    in.close();
    outputStream.close();
%>
<%!
    public static InputStream getInputStreamById(int imageFileId_para) {
        InputStream imagefile = null;
        try {
            RecordSet rs = new RecordSet();
            String sql = "select fileRealPath,isZip,isaesencrypt,aescode,tokenKey,storageStatus  from ImageFile where imageFileId = " + imageFileId_para;
            rs.executeQuery( sql );
            if (rs.next()) {
                String static_fileRealPath = Util.null2String( rs.getString( "fileRealPath" ) );
                String static_isZip = Util.null2String( rs.getString( "isZip" ) );
                int static_isaesencrypt = Util.getIntValue( rs.getString( "isaesencrypt" ), 0 );
                String static_aescode = Util.null2String( rs.getString( "aescode" ) );
                String static_tokenKey = Util.null2String( rs.getString( "tokenKey" ) );
                String static_storageStatus = Util.null2String( rs.getString( "storageStatus" ) );
                boolean isonlyAliOSS = false;
                if (!static_tokenKey.equals( "" ) && static_storageStatus.equals( "1" ) && AliOSSObjectManager.isonlyAliOSS()) {
                    isonlyAliOSS = true;
                }
                if (isonlyAliOSS) {
                    imagefile = weaver.alioss.AliOSSObjectUtil.downloadFile( static_tokenKey );
                } else {
                    if (static_fileRealPath != null && !static_fileRealPath.equals( "" )) {
                        File thefile = new File( static_fileRealPath );
                        if (static_isZip.equals( "1" )) {
                            ZipInputStream zin = new ZipInputStream( new FileInputStream( thefile ) );
                            if (zin.getNextEntry() != null)
                                imagefile = new BufferedInputStream( zin );
                        } else {
                            imagefile = new BufferedInputStream( new FileInputStream( thefile ) );
                        }
                    } else {
                        ConnStatement statement = null;
                        try {
                            statement = new ConnStatement();
                            boolean isoracle = ("oracle").equals( statement.getDBType() );
                            statement.setStatementSql( "select imagefile from ImageFile where imagefileid = " + imageFileId_para );
                            statement.executeQuery();
                            if (statement.next()) {
                                if (isoracle)
                                    imagefile = new BufferedInputStream( statement.getBlobBinary( "imagefile" ) );
                                else imagefile = new BufferedInputStream( statement.getBinaryStream( "imagefile" ) );
                            }
                        } catch (Exception ex) {
                        } finally {
                            try {
                                if (statement != null) statement.close();
                            } catch (Exception ex) {
                            }
                        }
                    }
                    if (static_isaesencrypt == 1) {
                        imagefile = AESCoder.decrypt( imagefile, static_aescode );
                    }

                }
            }


        } catch (Exception ex) {

        }
        return imagefile;
    }

%>
